<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>v-for 实例</title>
    <script type="text/javascript" src="../assets/js/vue.js"></script>
</head>
<body>
    <h1>v-for 实例</h1>
    <hr>
    <div id="app">
        <ul>
            <li v-for="item in SortItems">
                {{item}}
            </li>
        </ul>
        <hr>
        <ul>
            <li  v-for="(student,index) in sortStudents">
                {{index+1}}-姓名：{{student.name}}-年龄：{{student.age}}
            </li>
        </ul>
    </div>

    <script type="text/javascript">
        var app = new Vue({
            el:'#app',
            data:{
                items:[53,23,7,14,54,36,28],
                students:[
                    {name:'像素狮',age:'24'},
                    {name:'小明',age:'26'},
                    {name:'小张',age:'30'},
                    {name:'小华',age:'20'},
                    {name:'Tim',age:'15'},
                ]
            },
            computed:{
                SortItems:function(){
                    return this.items.sort(sortNumber);
                },
                sortStudents:function(){
                    return sortByKey(this.students,"age")
                }
            }

        })
        //数字排序a-b升序b-a降序
        function sortNumber(a,b){
            return a-b
        }
        //数组对象方法排序:
        function sortByKey(array,key){
                return array.sort(function(a,b){
                var x=a[key];
                var y=b[key];
                return ((x<y)?-1:((x>y)?1:0));
            });
        }
    </script>
</body>
</html>